﻿/*
 * tester: Jie Huang
 */

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AircraftBattleServer;

namespace ServerInterfaceTestProject
{
    /// <summary>
    /// Summary description for IGameEngineServiceTest1
    /// </summary>
    public partial class IGameEngineServiceTest
    {

        #region Test Cases for GetPosition

        //a.	id = -1: to  test negative (and not existing id) 
        //Expected: Proper exception thrown.
        //b.	id = 0: zero is usually considered as a boundary case
        //Expected: Should behave just like usual id
        //c.	Positive but not existing id
        //Expected: Proper exception should throw
        //d.	id = 99999999999999999 -- not applicable
        //Expected:  Proper exception should throw


        /// <summary>
        ///A test for GetPosition
        ///</summary>
        [TestMethod()]
        public void GetPositionTestA()
        {
            IGameEngineService target = CreateIGameEngineService(); 
            int id = -1;
            bool exceptionCaught = false;
            string expectedErrorMessage = "The given aircraft id does not exist";

            try
            {
                target.GetPosition(id);
            }
            catch (Exception ex)
            {
                exceptionCaught = true;
                Assert.AreEqual(expectedErrorMessage, ex.Message);
            }

            Assert.AreEqual(exceptionCaught, true);
        }

        /// <summary>
        ///A test for GetPosition
        ///</summary>
        [TestMethod()]
        public void GetPositionTestB()
        {
            IGameEngineService target = CreateIGameEngineService();
            int id = 0;
            bool exceptionCaught = false;
            string expectedErrorMessage = "The given aircraft id does not exist";

            try
            {
                target.GetPosition(id);
            }
            catch (Exception ex)
            {
                exceptionCaught = true;
                Assert.AreEqual(expectedErrorMessage, ex.Message);
            }

            Assert.AreEqual(exceptionCaught, true);
        }

        /// <summary>
        ///A test for GetPosition
        ///</summary>
        [TestMethod()]
        public void GetPositionTestC()
        {
            IGameEngineService target = CreateIGameEngineService();
            int id = 1000;
            bool exceptionCaught = false;
            string expectedErrorMessage = "The given aircraft id does not exist";

            try
            {
                target.GetPosition(id);
            }
            catch (Exception ex)
            {
                exceptionCaught = true;
                Assert.AreEqual(expectedErrorMessage, ex.Message);
            }

            Assert.AreEqual(exceptionCaught, true);
        }

        /// <summary>
        ///A test for GetPosition
        ///</summary>
        [TestMethod()]
        public void GetPositionTestD()
        {
            IGameEngineService target = CreateIGameEngineService();
            int id = 1;

            try
            {
                target.AddAircraft(id, new MyPoint(100, 100), 100);
            }
            catch
            {
                target.SetPosition(id, 100, 100);
            }

            MyPoint p = target.GetPosition(id);

            Assert.IsNotNull(p);
            Assert.AreEqual(p.x, 100);
            Assert.AreEqual(p.y, 100);
        }

        #endregion 

        #region Test Cases of SetPosition

        //a.	craftID = 0, left = 100, top = 100
        //Expected: If the craft already exists, throw proper exception. Otherwise, add corresponding object to the server data stucture
        //b.	craftID = -1 left = 100, top = 100
        //Expected: Throw proper exception indicating there is no such id
        //c.	existing craftID, left = -1, top = -1
        //Expected: Craft should be moved to destination successfully

        /// <summary>
        ///A test for SetPosition
        ///</summary>
        [TestMethod()]
        public void SetPositionTestA()
        {
            IGameEngineService target = CreateIGameEngineService();
            int craftId = 0; 
            double left = 100; 
            double top = 100; 
            bool exceptionCaught = false;
            string expectedErrorMessage = "Set position failed, probably because id does not exist";

            target.StartGame();

            try
            {
                target.SetPosition(craftId, left, top);
            }
            catch (Exception ex)
            {
                exceptionCaught = true;
                Assert.AreEqual(expectedErrorMessage, ex.Message);
            }

            Assert.AreEqual(exceptionCaught, true);            
        }

        /// <summary>
        ///A test for SetPosition
        ///</summary>
        [TestMethod()]
        public void SetPositionTestB()
        {
            IGameEngineService target = CreateIGameEngineService();
            int craftId = -1;
            double left = 100;
            double top = 100;
            bool exceptionCaught = false;
            string expectedErrorMessage = "Craft id can not be negative value";

            target.StartGame();

            try
            {
                target.SetPosition(craftId, left, top);
            }
            catch (Exception ex)
            {
                exceptionCaught = true;
                Assert.AreEqual(expectedErrorMessage, ex.Message);
            }

            Assert.AreEqual(exceptionCaught, true);  
        }

        /// <summary>
        ///A test for SetPosition
        ///</summary>
        [TestMethod()]
        public void SetPositionTestC()
        {
            GameEngineService target = (GameEngineService) CreateIGameEngineService();

            int craftId = 1;
            double left = -1;
            double top = -1;

            try
            {
                target.AddAircraft(craftId, new MyPoint(100, 100), 100);
            }
            catch
            {
                target.SetPosition(craftId, 100, 100);
            }

            target.SetPosition(craftId, left, top);
            MyPoint p = target.GetPosition(craftId);

            Assert.IsNotNull(p);
            Assert.AreEqual(p.x, -1);
            Assert.AreEqual(p.y, -1);
        }

        #endregion

        #region Test Cases of SetAngle

        //a.	craftID = 0, angle = 0
        //Expected: Behave correctly according whether  id 0 exists
        //b.	existing craftID, angle > 360
        //Expected: The angle should be changed properly
        //c.	existing craftID, angle = 360
        //Expected: The angle should be changed properly
        //d.	craftID = -1, angle = 0
        //Expected: Proper exception should throw
        //e.	existing craftID, angle = -180
        //Expected: The angle should be changed properly

        /// <summary>
        ///A test for SetAngle
        ///</summary>
        [TestMethod()]
        public void SetAngleTestA()
        {
            IGameEngineService target = CreateIGameEngineService(); 
            int craftId = 0; 
            double angle = 0;
            bool exceptionCaught = false;
            string expectedErrorMessage = "SetAngle failed, probably because id does not exist";

            target.StartGame();

            try
            {
                target.SetAngle(craftId, angle);
            }
            catch (Exception ex)
            {
                exceptionCaught = true;
                Assert.AreEqual(expectedErrorMessage, ex.Message);
            }

            Assert.AreEqual(exceptionCaught, true); 
        }

        [TestMethod()]
        public void SetAngleTestB()
        {
            IGameEngineService target = CreateIGameEngineService(); 
            int craftId = 1; 
            double angle = 720;

            target.StartGame();

            try
            {
                target.AddAircraft(craftId, new MyPoint(100, 100), 100);
            }
            catch
            {
                target.SetPosition(craftId, 100, 100);
            }

            target.SetAngle(craftId, angle);
            MapData map = target.GetMapData();
            Assert.IsNotNull(map);
            Assert.AreEqual(map.aircrafts[craftId].angle, angle);
        }

        [TestMethod()]
        public void SetAngleTestC()
        {
            IGameEngineService target = CreateIGameEngineService();
            int craftId = 1;
            double angle = 360;

            target.StartGame();

            try
            {
                target.AddAircraft(craftId, new MyPoint(100, 100), 100);
            }
            catch
            {
                target.SetPosition(craftId, 100, 100);
            }

            target.SetAngle(craftId, angle);
            MapData map = target.GetMapData();
            Assert.IsNotNull(map);
            Assert.AreEqual(map.aircrafts[craftId].angle, angle);
        }

        [TestMethod()]
        public void SetAngleTestD()
        {
            IGameEngineService target = CreateIGameEngineService();
            int craftId = -1;
            double angle = 0;
            bool exceptionCaught = false;
            string expectedErrorMessage = "Craft id cannot be negative value";

            target.StartGame();

            try
            {
                target.SetAngle(craftId, angle);
            }
            catch (Exception ex)
            {
                exceptionCaught = true;
                Assert.AreEqual(expectedErrorMessage, ex.Message);
            }

            Assert.AreEqual(exceptionCaught, true);
        }

        [TestMethod()]
        public void SetAngleTestE()
        {
            IGameEngineService target = CreateIGameEngineService();
            int craftId = 1;
            double angle = -180;

            GameEngineService.gameStarted = false;
            target.SetAngle(craftId, angle);

            target.StartGame();

            try
            {
                target.AddAircraft(craftId, new MyPoint(100, 100), 100);
            }
            catch
            {
                target.SetPosition(craftId, 100, 100);
            }

            target.SetAngle(craftId, angle);
            MapData map = target.GetMapData();
            Assert.IsNotNull(map);
            Assert.AreEqual(map.aircrafts[craftId].angle, angle);
        }

        #endregion
    }
}
